﻿@namespace Oqtane.Modules.Admin.Pages
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IPageService PageService
@inject IThemeService  ThemeService

<TabStrip>
    <TabPanel Name="Settings">
        @if (_themeList != null)
        {
            <table class="table table-borderless">
                <tr>
                    <td>
                        <Label For="Name" HelpText="Enter the page name">Name: </Label>
                    </td>
                    <td>
                        <input id="Name" class="form-control" @bind="@_name" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <Label For="Parent" HelpText="Select the parent for the page in the site hierarchy">Parent: </Label>
                    </td>
                    <td>
                        <select id="Parent" class="form-control" @onchange="(e => ParentChanged(e))">
                            <option value="-1">&lt;Site Root&gt;</option>
                            @foreach (Page page in _pageList)
                            {
                                if (page.PageId.ToString() == _parentid)
                                {
                                    <option value="@(page.PageId)" selected>@(new string('-', page.Level * 2))@(page.Name)</option>
                                }
                                else
                                {
                                    <option value="@(page.PageId)">@(new string('-', page.Level * 2))@(page.Name)</option>
                                }
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <Label For="Move" HelpText="Select the location where you would like the page to be moved in relation to other pages">Move: </Label>
                    </td>
                    <td>
                        <select id="Move" class="form-control" @bind="@_insert">
                            @if (_parentid == _currentparentid)
                            {
                                <option value="=">&lt;Maintain Current Location&gt;</option>
                            }
                            <option value="<<">To Beginning</option>
                            @if (_children != null && _children.Count > 0)
                            {
                                <option value="<">Before</option>
                                <option value=">">After</option>
                            }
                            <option value=">>">To End</option>
                        </select>
                        @if (_children != null && _children.Count > 0 && (_insert == "<" || _insert == ">"))
                        {
                            <select class="form-control" @bind="@_childid">
                                <option value="-1">&lt;Select Page&gt;</option>
                                @foreach (Page page in _children)
                                {
                                    <option value="@(page.PageId)">@(page.Name)</option>
                                }
                            </select>
                        }
                    </td>
                </tr>
                <tr>
                    <td>
                        <Label For="Navigation" HelpText="Select whether the page is part of the site navigation or hidden">Navigation? </Label>
                    </td>
                    <td>
                        <select id="Navigation" class="form-control" @bind="@_isnavigation">
                            <option value="True">Yes</option>
                            <option value="False">No</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <Label For="Path" HelpText="Optionally enter a url path for this page (ie. home ). If you do not provide a url path, the page name will be used.">Url Path: </Label>
                    </td>
                    <td>
                        <input id="Path" class="form-control" @bind="@_path" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <Label For="Url" HelpText="Optionally enter a url which this page should redirect to when a user navigates to it">Redirect: </Label>
                    </td>
                    <td>
                        <input id="Url" class="form-control" @bind="@_url" />
                    </td>
                </tr>
            </table>
            <Section Name="Appearance">
                <table class="table table-borderless">
                    <tr>
                        <td>
                            <Label For="Title" HelpText="Optionally enter the page title. If you do not provide a page title, the page name will be used.">Title: </Label>
                        </td>
                        <td>
                            <input id="Title" class="form-control" @bind="@_title" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <Label For="Theme" HelpText="Select the theme for this page">Theme: </Label>
                        </td>
                        <td>
                            <select id="Theme" class="form-control" @onchange="(e => ThemeChanged(e))">
                                <option value="-">&lt;Inherit From Site&gt;</option>
                                @foreach (var theme in _themes)
                                {
                                    if (theme.TypeName == _themetype)
                                    {
                                        <option value="@theme.TypeName" selected>@theme.Name</option>
                                    }
                                    else
                                    {
                                        <option value="@theme.TypeName">@theme.Name</option>
                                    }
                                }
                            </select>
                        </td>
                    </tr>
                    @if (_layouts.Count > 0)
                    {
                        <tr>
                            <td>
                                <Label For="Layout" HelpText="Select a layout for the page (if the selected theme supports it)">Layout: </Label>
                            </td>
                            <td>
                                <select id="Layout" class="form-control" @bind="@_layouttype">
                                    <option value="-">&lt;Inherit From Site&gt;</option>
                                    @foreach (var layout in _layouts)
                                    {
                                        if (layout.TypeName == _layouttype)
                                        {
                                            <option value="@(layout.TypeName)" selected>@(layout.Name)</option>
                                        }
                                        else
                                        {
                                            <option value="@(layout.TypeName)">@(layout.Name)</option>
                                        }
                                    }
                                </select>
                            </td>
                        </tr>
                    }
                    <tr>
                        <td>
                            <Label For="defaultContainer" HelpText="Select the default container for the page">Default Container: </Label>
                        </td>
                        <td>
                            <select id="defaultContainer" class="form-control" @bind="@_containertype">
                                <option value="-">&lt;Inherit From Site&gt;</option>
                                @foreach (var container in _containers)
                                {
                                    <option value="@container.TypeName">@container.Name</option>
                                }
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <Label For="Icon" HelpText="Optionally provide an icon for this page which will be displayed in the site navigation">Icon: </Label>
                        </td>
                        <td>
                            <input id="Icon" class="form-control" @bind="@_icon" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <Label For="Default-Mode" HelpText="Select the default administration mode you want for this page">Default Mode? </Label>
                        </td>
                        <td>
                            <select id="Default-Mode" class="form-control" @bind="@_mode">
                                <option value="view">View Mode</option>
                                <option value="edit">Edit Mode</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <Label For="Personalizable" HelpText="Select whether you would like users to be able to personalize this page with their own content">Personalizable? </Label>
                        </td>
                        <td>
                            <select id="Personalizable" class="form-control" @bind="@_ispersonalizable">
                                <option value="True">Yes</option>
                                <option value="False">No</option>
                            </select>
                        </td>
                    </tr>
                </table>
            </Section>
            <br /><br />
            <AuditInfo CreatedBy="@_createdby" CreatedOn="@_createdon" ModifiedBy="@_modifiedby" ModifiedOn="@_modifiedon" DeletedBy="@_deletedby" DeletedOn="@_deletedon"></AuditInfo>
        }
    </TabPanel>
    <TabPanel Name="Permissions">
        @if (_permissions != null)
        {
            <table class="table table-borderless">
                <tr>
                    <td>
                        <PermissionGrid EntityName="@EntityNames.Page" Permissions="@_permissions" @ref="_permissionGrid" />
                    </td>
                </tr>
            </table>
        }
    </TabPanel>
</TabStrip>
<button type="button" class="btn btn-success" @onclick="SavePage">Save</button>
<NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>

@code {
    private List<Theme> _themeList;
    private List<ThemeControl> _themes = new List<ThemeControl>();
    private List<ThemeControl> _layouts = new List<ThemeControl>();
    private List<ThemeControl> _containers = new List<ThemeControl>();
    private List<Page> _pageList;
    private int _pageId;
    private string _name;
    private string _title;
    private string _path;
    private string _currentparentid;
    private string _parentid;
    private string _insert = "=";
    private List<Page> _children;
    private int _childid = -1;
    private string _isnavigation;
    private string _url;
    private string _ispersonalizable;
    private string _mode;
    private string _themetype = "-";
    private string _layouttype = "-";
    private string _containertype = "-";
    private string _icon;
    private string _permissions = null;
    private string _createdby;
    private DateTime _createdon;
    private string _modifiedby;
    private DateTime _modifiedon;
    private string _deletedby;
    private DateTime? _deletedon;

#pragma warning disable 649
    private PermissionGrid _permissionGrid;
#pragma warning restore 649

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _pageList = PageState.Pages;
            _children = PageState.Pages.Where(item => item.ParentId == null).ToList();

            _themeList = await ThemeService.GetThemesAsync();
            _themes = ThemeService.GetThemeControls(_themeList);

            _pageId = Int32.Parse(PageState.QueryString["id"]);
            var page = PageState.Pages.FirstOrDefault(item => item.PageId == _pageId);
            if (page != null)
            {
                _name = page.Name;
                _title = page.Title;
                _path = page.Path;

                if (_path.Contains("/"))
                {
                    _path = _path.Substring(_path.LastIndexOf("/") + 1);
                }

                if (page.ParentId == null)
                {
                    _parentid = string.Empty;
                }
                else
                {
                    _parentid = page.ParentId.ToString();
                }

                _currentparentid = _parentid;
                _isnavigation = page.IsNavigation.ToString();
                _url = page.Url;
                _ispersonalizable = page.IsPersonalizable.ToString();
                _mode = (page.EditMode) ? "edit" : "view";
                _themetype = page.ThemeType;
                if (_themetype == PageState.Site.DefaultThemeType)
                {
                    _themetype = "-";
                }
                _layouts = ThemeService.GetLayoutControls(_themeList, page.ThemeType);
                _layouttype = page.LayoutType;
                if (_layouttype == PageState.Site.DefaultLayoutType)
                {
                    _layouttype = "-";
                }
                _containers = ThemeService.GetContainerControls(_themeList, page.ThemeType);
                _containertype = page.DefaultContainerType;
                if (string.IsNullOrEmpty(_containertype))
                {
                    _containertype = "-";
                }
                _icon = page.Icon;
                _permissions = page.Permissions;
                _createdby = page.CreatedBy;
                _createdon = page.CreatedOn;
                _modifiedby = page.ModifiedBy;
                _modifiedon = page.ModifiedOn;
                _deletedby = page.DeletedBy;
                _deletedon = page.DeletedOn;
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Page {PageId} {Error}", _pageId, ex.Message);
            AddModuleMessage("Error Loading Page", MessageType.Error);
        }
    }

    private async void ParentChanged(ChangeEventArgs e)
    {
        try
        {
            _parentid = (string)e.Value;
            _children = new List<Page>();
            if (_parentid == "-1")
            {
                foreach (Page p in PageState.Pages.Where(item => item.ParentId == null))
                {
                    if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, p.Permissions))
                    {
                        _children.Add(p);
                    }
                }
            }
            else
            {
                foreach (Page p in PageState.Pages.Where(item => item.ParentId == int.Parse(_parentid)))
                {
                    if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, p.Permissions))
                    {
                        _children.Add(p);
                    }
                }
            }
            if (_parentid == _currentparentid)
            {
                _insert = "=";
            }
            else
            {
                _insert = ">>";
            }
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Child Pages For Parent {PageId} {Error}", _parentid, ex.Message);
            AddModuleMessage("Error Loading Child Pages For Parent", MessageType.Error);
        }
    }

    private async void ThemeChanged(ChangeEventArgs e)
    {
        try
        {
            _themetype = (string)e.Value;
            if (_themetype != "-")
            {
                _layouts = ThemeService.GetLayoutControls(_themeList, _themetype);
                _containers = ThemeService.GetContainerControls(_themeList, _themetype);
            }
            else
            {
                _layouts = new List<ThemeControl>();
                _containers = new List<ThemeControl>();
            }
            _layouttype = "-";
            _containertype = "-";
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Pane Layouts For Theme {ThemeType} {Error}", _themetype, ex.Message);
            AddModuleMessage("Error Loading Pane Layouts For Theme", MessageType.Error);
        }
    }

    private async Task SavePage()
    {
        Page page = null;
        try
        {
            if (_name != string.Empty)
            {
                page = PageState.Pages.FirstOrDefault(item => item.PageId == _pageId);
                string currentPath = page.Path;

                page.Name = _name;
                page.Title = _title;
                if (_path == "" && _name.ToLower() != "home")
                    if (_path == string.Empty && _name.ToLower() != "home")
                    {
                        _path = _name;
                    }
                if (_path.Contains("/"))
                {
                    _path = _path.Substring(_path.LastIndexOf("/") + 1);
                }
                if (string.IsNullOrEmpty(_parentid) || _parentid == "-1")
                {
                    page.ParentId = null;
                    page.Path = Utilities.GetFriendlyUrl(_path);
                }
                else
                {
                    page.ParentId = Int32.Parse(_parentid);
                    Page parent = PageState.Pages.FirstOrDefault(item => item.PageId == page.ParentId);
                    if (parent.Path == string.Empty)
                    {
                        page.Path = Utilities.GetFriendlyUrl(parent.Name) + "/" + Utilities.GetFriendlyUrl(_path);
                    }
                    else
                    {
                        page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path);
                    }
                }

                if (!PagePathIsUnique(page.Path, page.SiteId, page.PageId, _pageList))
                {
                    AddModuleMessage($"A page with path {_path} already exists for the selected parent page. The page path needs to be unique for the selected parent.", MessageType.Warning);
                    return;
                }

                if (_insert != "=")
                {
                    Page child;
                    switch (_insert)
                    {
                        case "<<":
                            page.Order = 0;
                            break;
                        case "<":
                            child = PageState.Pages.FirstOrDefault(item => item.PageId == _childid);
                            if (child != null) page.Order = child.Order - 1;
                            break;
                        case ">":
                            child = PageState.Pages.FirstOrDefault(item => item.PageId == _childid);
                            if (child != null) page.Order = child.Order + 1;
                            break;
                        case ">>":
                            page.Order = int.MaxValue;
                            break;
                    }
                }
                page.IsNavigation = (_isnavigation == null || Boolean.Parse(_isnavigation));
                page.Url = _url;
                page.EditMode = (_mode == "edit");
                page.ThemeType = (_themetype != "-") ? _themetype : string.Empty;
                if (!string.IsNullOrEmpty(page.ThemeType) && page.ThemeType == PageState.Site.DefaultThemeType)
                {
                    page.ThemeType = string.Empty;
                }
                page.LayoutType = (_layouttype != "-") ? _layouttype : string.Empty;
                if (!string.IsNullOrEmpty(page.LayoutType) && page.LayoutType == PageState.Site.DefaultLayoutType)
                {
                    page.LayoutType = string.Empty;
                }
                page.DefaultContainerType = (_containertype != "-") ? _containertype : string.Empty;
                if (!string.IsNullOrEmpty(page.DefaultContainerType) && page.DefaultContainerType == PageState.Site.DefaultContainerType)
                {
                    page.DefaultContainerType = string.Empty;
                }
                page.Icon = _icon ?? string.Empty;
                page.Permissions = _permissionGrid.GetPermissions();
                page.IsPersonalizable = (_ispersonalizable != null && Boolean.Parse(_ispersonalizable));
                page.UserId = null;

                page = await PageService.UpdatePageAsync(page);
                await PageService.UpdatePageOrderAsync(page.SiteId, page.PageId, page.ParentId);
                if (_currentparentid == string.Empty)
                {
                    await PageService.UpdatePageOrderAsync(page.SiteId, page.PageId, null);
                }
                else
                {
                    await PageService.UpdatePageOrderAsync(page.SiteId, page.PageId, int.Parse(_currentparentid));
                }

                // update child paths
                if (_parentid != _currentparentid)
                {
                    foreach (Page p in PageState.Pages.Where(item => item.Path.StartsWith(currentPath)))
                    {
                        p.Path = p.Path.Replace(currentPath, page.Path);
                        await PageService.UpdatePageAsync(p);
                    }
                }

                await logger.LogInformation("Page Saved {Page}", page);
                NavigationManager.NavigateTo(NavigateUrl(page.Path));
            }
            else
            {
                AddModuleMessage("You Must Provide Page Name", MessageType.Warning);
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Saving Page {Page} {Error}", page, ex.Message);
            AddModuleMessage("Error Saving Page", MessageType.Error);
        }
    }

    private static bool PagePathIsUnique(string pagePath, int siteId, int pageId, List<Page> existingPages)
    {
        return !existingPages.Any(page => page.SiteId == siteId && page.Path == pagePath && page.PageId != pageId);
    }
}
